package com.xique.common.core.utils;

import com.xique.common.core.constant.CacheConstants;
import com.xique.common.core.text.Convert;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 权限获取工具类
 *
 * @author xique
 */
public class SecurityUtils
{
    /**
     * 获取用户
     */
    public static String getUsername()
    {
        String username = Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_USERNAME);
        return ServletUtils.urlDecode(username == null ? "" : username);
    }

    /**
     * 获取用户ID
     */
    public static Long getBaseUserId()
    {
        return Convert.toLong(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_BASE_USER_ID));
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId()
    {
        return Convert.toLong(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_USER_ID));
    }

    /**
     * 获取用户类型
     */
    public static String getUserType()
    {
        return Convert.toStr(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_USER_TYPE));
    }

    public static String getPlatformType()
    {
        return Convert.toStr(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_PLATFORM_TYPE));
    }

    /**
     * 获取项目ID
     */
    public static Long getParkId() {
        return Convert.toLong(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_PARK_ID));
    }

    /**
     * 获取岗亭ID
     */
    public static Long getClientId() {
        return Convert.toLong(Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.DETAILS_CLIENT_ID));
    }

    /**
     * 获取登录日志ID
     */
    public static Long getLoginInfoId() {
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_LOGIN_INFO_ID));
    }

    public static Long getForeignId() {
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.FOREIGN_ID));
    }

    public static String getForeignType() {
        return Convert.toStr(ServletUtils.getRequest().getHeader(CacheConstants.FOREIGN_TYPE));
    }

    public static String getForeignName() {
        return Convert.toStr(ServletUtils.getRequest().getHeader(CacheConstants.FOREIGN_NAME));
    }

    public static String getOpenId() {
        return Convert.toStr(ServletUtils.getRequest().getHeader(CacheConstants.OPEN_ID));
    }

    public static String getUnionId() {
        return Convert.toStr(ServletUtils.getRequest().getHeader(CacheConstants.UNION_ID));
    }

    public static String getAppType() {
        return Convert.toStr(ServletUtils.getRequest().getHeader(CacheConstants.APP_TYPE));
    }

    public static String getZfbUserId()
    {
        String zfbUserId = Objects.requireNonNull(ServletUtils.getRequest()).getHeader(CacheConstants.ZFB_USER_ID);
        return ServletUtils.urlDecode(zfbUserId == null ? "" : zfbUserId);
    }


    /**
     * 获取请求token
     */
    public static String getToken()
    {
        return getToken(ServletUtils.getRequest());
    }

    /**
     * 根据request获取请求token
     */
    public static String getToken(HttpServletRequest request)
    {
        String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER);
        if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
        {
            token = token.replace(CacheConstants.TOKEN_PREFIX, "");
        }
        return token;
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword 真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword 真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPasswordNoEncoded(String rawPassword, String encodedPassword)
    {
        return Objects.equals(rawPassword, encodedPassword);
    }

    public static String encryptPasswordWithOldVersion(String username, String password, String salt)
    {
        return DigestUtils.md5DigestAsHex((username + password + salt).getBytes());
    }

    public static boolean matchesPasswordWithOldVersion(String rawPassword, String loginName, String salt, String encodedPassword)
    {
        return Objects.equals(encryptPasswordWithOldVersion(loginName, rawPassword, salt), encodedPassword);
    }
}
